/*
 * $Id: Control.java 87 2009-04-23 16:21:31Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.gui.deltagui.widget;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.concurrent.atomic.AtomicBoolean;


/**
 * Control.
 * 
 * @author Andreas Meyer
 * @version $Revision: 87 $
 */
public class Control extends Widget {
	
	/** The focused control. */
	static Control focusedControl;

	/** The parent composite of this control. */
	private Composite parent;
	
	/** The flag if the control needs to be rendered. */
	protected AtomicBoolean rendered = new AtomicBoolean(false);

	/** The flag if this control has the focus. */
	private AtomicBoolean focus = new AtomicBoolean(false);

	
	/**
	 * @return the parent
	 */
	public Composite getParent() {
		return parent;
	}

	/**
	 * @param parent the parent to set
	 */
	public void setParent(final Composite parent) {
		this.parent = parent;
	}

	/**
	 * Invalidate the control.
	 */
	protected void invalidate() {
		parent.invalidate();
	}

	/**
	 * Mark the control as rendered.
	 * 
	 */
	protected void setRendered() {
		this.rendered.set(true);
	}

	/**
	 * Mark the control as not rendered.
	 * 
	 */
	protected void setUnrendered() {
		this.rendered.set(false);
	}
	
	/**
	 * Render the control and all child controls.
	 * 
	 * @param g2
	 */
	public void render(final Graphics2D g2) {

		if(!rendered.get()) {

			if(getBackgroundColor() != null) {

				g2.setColor(getBackgroundColor());
				final Rectangle r = g2.getClipBounds();
				g2.fillRect(r.x, r.y, r.width, r.height);
			} // end if

			renderControl(g2);

			if(getBorderColor() != null) {

				g2.setColor(getBorderColor());
				final Rectangle r = g2.getClipBounds();
				g2.drawRect(r.x, r.y, r.width - 1, r.height - 1);
			} // end if

			setRendered();
		}
	}
	
	/**
	 * Render the control.
	 * 
	 * @param g2
	 */
	public void renderControl(final Graphics2D g2) {

		// Sub classes override this to render the controls
	}

	/**
	 * @return the focus
	 */
	public boolean hasFocus() {
		return focus.get();
	}
	
	/**
	 * Return if the widget id focusable.
	 * 
	 * @return
	 */
	public boolean isFocusable() {
		return true;
	}
	
	/**
	 * Set the focus state to false.
	 * 
	 */
	void setUnfocused() {
		focus.set(false);
		setUnrendered();
	}

	/**
	 * Set the focus to this control or delegate it to the first child.
	 * 
	 * @return
	 */
	void setFocus() {
		
		focus.set(true);
		Control.focusedControl = this;
		setUnrendered();
	}

	/**
	 * Post a key event.
	 * 
	 * @param ke
	 */
	protected void postKeyEvent(final KeyEvent ke) {

		//TODO: implement
	}

}
